package xyz.luckapp.web;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import xyz.luckapp.entity.Json;
import xyz.luckapp.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController extends ControllerAll{
	@Resource
	private UserService userService;
	
	@RequestMapping("/login.do")
	@ResponseBody
	public Object login(String account,String password,HttpServletResponse response){
		List<Map<String,Object>> list=userService.getLoginInfo(account, password);
		for(Map<String,Object> map:list){
			String id=(String) map.get("id");
			String name=(String) map.get("name");
			Cookie cookieId=new Cookie("userId",id);
			Cookie cookieName=new Cookie("userName",name);
			cookieId.setPath("/");
			cookieName.setPath("/");
			response.addCookie(cookieId);
			response.addCookie(cookieName);
		}
		return new Json(list);
	}
	@RequestMapping("/save.do")
	@ResponseBody
	public Object save(String account,String password,String pwdto){
		userService.save(account, password,pwdto);
		return new Json(1,null,"SUCCESS");
	}
}
